package com.task.src.controller;

import com.task.src.dto.MyTask;
import com.task.src.dto.Task;
import com.task.src.mapper.MyTaskMapper;
import com.task.src.mapper.TaskMapper;
import com.task.src.service.MyTaskService;
import com.task.src.service.TaskService;
import com.task.src.util.ResponseResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/myTask")
public class MyTaskController {
    @Resource
    private MyTaskService myTaskService;

    @Resource
    private TaskService taskService;

    @Resource
    private TaskMapper taskMapper;

    @Resource
    private MyTaskMapper myTaskMapper;

    @Resource
    private HttpSession session;
    @RequestMapping("/all")
    public ResponseResult selectMyt(HttpSession session){
        ResponseResult res=new ResponseResult();
        int userid = (int)session.getAttribute("userid");
        List<MyTask> list=myTaskService.selectMyT(userid);
        if(list==null){
            res.setCode(303);
            return res;
        }
        res.setData(list);
        res.setCode(200);
        return res;
    }

    @RequestMapping("/access")
    public ResponseResult accessTask(HttpSession session,int taskid){
        ResponseResult res=new ResponseResult();
        int userid=(int)session.getAttribute("userid");
        int i=myTaskService.insertMyTask(userid,taskid);
        if(i!=0){
            taskService.updateType(taskid);
            res.setCode(200);
            return res;
        }
        res.setCode(500);
        return res;
    }
    @RequestMapping("/giveUp")
    public ResponseResult giveUp(int taskid,int mytaskid){
        ResponseResult res=new ResponseResult();
        taskMapper.updateStatus(taskid);
        myTaskMapper.deleteMyTask(mytaskid);
        res.setCode(200);
        return res;
    }

    @RequestMapping("/publish")
    public ResponseResult publish(){
        ResponseResult res=new ResponseResult();
        int uid= (int) session.getAttribute("userid");
        List<Task> list=myTaskMapper.selectMyPubLis(uid);
        res.setData(list);
        return res;
    }

    @RequestMapping("/myWc")
    public ResponseResult selectMyWc(){
        ResponseResult res=new ResponseResult();
        int userid = (int)session.getAttribute("userid");
        List<MyTask> list = myTaskMapper.selectMyWc(userid);
        if(list==null){
            res.setCode(303);
            return res;
        }
        res.setData(list);
        res.setCode(200);
        return res;
    }
}
